package com.yonyou.gmmc.service.points.eventhandle;


import org.marketing.common.dto.EventDataDTO;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventActivityInfoReadDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.util.MapConverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yonyou.gmmc.service.points.exception.PointsException;
import com.yonyou.gmmc.service.points.model.TmPointsRule;
import com.yonyou.gmmc.service.points.service.PointsPayService;
import com.yonyou.gmmc.service.points.service.PointsRuleService;
import com.yonyou.gmmc.service.points.util.SpringUtil;


/**
 * 阅读达到阈值事件
 * 
 * @author BENJAMIN
 *
 */
public class ActivityInfoReadEventHandler extends AbstractEventHandle {

	private static Logger logger = LoggerFactory.getLogger(ActivityInfoReadEventHandler.class);

	// 活动资讯
	private static EventBizType BizType = EventConstant.EventBizType.ACTIVITY_INFO;
	// 活动资讯阅读达到阈值
	private static EventBizStep BizStep = EventConstant.EventBizStep.ACTIVITY_INFO_READ;

	// @Autowired
	// private PointsRuleService pointsRuleService;
	
//    @Autowired
//    PointsPayService pointsPayService;
	
	@Override
	protected String getHandleInfo() {
		return "处理活动资讯阅读达到阈值事件节点";
	}

	
	/**
	 * 主要进行数据转换
	 */
	@Override
	protected EventActivityInfoReadDTO preTransform(BizEvent event) {
		EventActivityInfoReadDTO o = new EventActivityInfoReadDTO();
		try {
			o = (EventActivityInfoReadDTO) MapConverUtil.map2POStrict(event.getEventData(), o);
			logger.info(o.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			this.backInfo = "eventData中的业务数据不能识别：" + e.getMessage();
			throw new PointsException(PointsException.EVENT_CONVERT_FAILD,"eventData中的业务数据不能识别");
		}
		return o;
	}

	/**
	 * 积分处理的逻辑
	 */
	@Override
	protected boolean pay(EventDataDTO eventData) throws PointsException{
		
		//获取到业务数据
		EventActivityInfoReadDTO thisEventData = (EventActivityInfoReadDTO)eventData;
		
		PointsRuleService pointsRuleService=(PointsRuleService)SpringUtil.getBean("pointsRuleService");
		
		
		PointsPayService pointsPayService=(PointsPayService)SpringUtil.getBean("pointsPayService");
		
		//获取到规则
		TmPointsRule rule = pointsRuleService.getRuleByBIZ(BizType.getCodeLong(), BizStep.getCodeLong());

		//查詢每日閱讀獲取的積分數
		logger.info("開始查詢積分數");
		int num = 0;
        num = pointsPayService.getCustomerLog(thisEventData);
        logger.info("查询每日分享积分数——"+num);
        if(num<10){
            //阅读积分每天不超过10分
    		if(rule!=null){
    			Long points = rule.getPointsNum();
    			
    			
    			pointsPayService.payPointsToUser(points, 
    					thisEventData.getUserId(),
    					thisEventData.getPhone(),
    					//this.getHandleInfo(),
    					rule.getRemark(),	//2017 4 13修改为规则表的remark
    					String.valueOf(thisEventData.getInfoId()),
    					BizStep.getCode(),
    					thisEventData.getPhone()
    					);
    		}else{
    			this.backInfo = "未找到对应的积分规则：";
    			throw new PointsException(PointsException.RULE_NOTFOUND,"未找到对应的积分规则");
    		} 
    	}
        else{
            logger.info("今日分享获取积分已到达10分,不再加分");
        }
		return false;
	}


}
